package org.iteye.bbjava.util;

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;

public class AjaxXmlUtil {
	
	public static Document createDocumentFromRequest(HttpServletRequest request)
			throws Exception {
		Document doc = null;
		try {

			InputStream is = request.getInputStream();

			byte[] b = new byte[request.getContentLength()]; // 开辟文件大小的空间

			int len = 0;

			int temp = 0;

			while ((temp = is.read()) != -1) {
				b[len] = (byte) temp;
				len++;
			}

			is.close();

			String xmlStr = new String(b, 0, len, "UTF-8");
			doc = DocumentHelper.parseText(xmlStr);

			System.out.println("读取到的字符串为：" + xmlStr);

			/*
			 * request.setCharacterEncoding("UTF-8");
			 * 
			 * int binArrayCount = request.getContentLength();
			 * 
			 * binArrayData = new byte[binArrayCount]; int j; for (int i = 0; i
			 * < binArrayCount; i += j) { try { j =
			 * request.getInputStream().read(binArrayData, i, binArrayCount -
			 * i); } catch (Exception e) { e.printStackTrace(); throw new
			 * Exception("=========创建Doc出错========="); } } String xmlStr = new
			 * String(binArrayData,"UTF-8");
			 * doc = DocumentHelper.parseText(xmlStr);
			 */
		} catch (Exception e) {
			// throw e;
			System.out.println("=====xml异常=====");
		}
		return doc;
	}

	public static void responseXML(HttpServletResponse response, String docXML)
			throws Exception {
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		pw = response.getWriter();
		pw.print(docXML);
		pw.close();

	}

	public static void transform(HttpServletRequest request,
			HttpServletResponse response, Document doc, String xslFileFullPath) {
		try {

			StreamSource xmlSource = new StreamSource(new StringReader(doc
					.getRootElement().asXML()));

			TransformerFactory tFac = TransformerFactory.newInstance();
			Source xslSource = new StreamSource(xslFileFullPath);
			Transformer t = tFac.newTransformer(xslSource);

			Result result = new StreamResult(System.out);

			t.transform(xmlSource, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

	public static String getStr(String str) {
		try {
			String temp_p = str;
			byte[] temp_t = temp_p.getBytes("ISO8859-1");
			String temp = new String(temp_t);
			return temp;
		} catch (Exception e) {

		}
		return "null";
	}
}
